package com.example.aiquestions.controller;

import com.example.aiquestions.entity.QuestionLibrary;
import com.example.aiquestions.model.common.ApiResponse;
import com.example.aiquestions.model.dto.QuestionDetail;
import com.example.aiquestions.service.QuestionLibraryService;
import com.example.aiquestions.service.QuestionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/libraries")
public class QuestionLibraryController {
    @Autowired
    private QuestionLibraryService libraryService;
    @Autowired
    private QuestionService questionService;

    /**
     *         PageInfo 对象包含了丰富的分页信息：
     *         pageNum：当前页码
     *         pageSize：每页条数
     *         size：当前页记录数
     *         total：总记录数
     *         pages：总页数
     *         list：数据列表
     *         isFirstPage：是否第一页
     *         isLastPage：是否最后一页
     *         hasPreviousPage：是否有上一页
     *         hasNextPage：是否有下一页
     */

    /**
     * 获取用户在指定学科下的所有题库
     * @param userId 用户id
     * @param subjectId 学科id
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页的题库列表
     */
    @GetMapping("/getLibraries")
    public ApiResponse<PageInfo<QuestionLibrary>> getLibraries(
            @RequestParam("userId") Long userId,
            @RequestParam("subjectId") Long subjectId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<QuestionLibrary> pageInfo = libraryService.getLibraryListByUserAndSubject(userId, subjectId, pageNum, pageSize);
        return ApiResponse.success(pageInfo);
    }

    /**
     * 创建题库
     * @param userId 用户id
     * @param subjectId 学科id
     * @param name 题库名称
     * @param ids 题目id列表
     * @return 创建的题库信息
     */
    @GetMapping("/createLibrary")
    public ApiResponse<QuestionLibrary> createLibrary(
            @RequestParam("userId") Long userId,
            @RequestParam("subjectId") Long subjectId,
            @RequestParam("name") String name,
            @RequestParam("ids") List<Long> ids) {
        QuestionLibrary library = libraryService.createLibrary(userId, subjectId, name, ids);
        return ApiResponse.success(library);
    }

    /**
     * 更新题库
     * @param libraryId 题库id
     * @param name 题库名称
     * @param subjectId 学科id
     * @return 更新后的题库信息
     */
    @GetMapping("/updateLibrary")
    public ApiResponse<QuestionLibrary> updateLibrary(
            @RequestParam("libraryId") String libraryId,
            @RequestParam("name") String name,
            @RequestParam("subjectId") Long subjectId) {
        QuestionLibrary library = libraryService.updateLibrary(libraryId, name, subjectId);
        return ApiResponse.success(library);
    }

    /**
     * 获取指定学科下的所有题库
     * @param subjectId 学科id
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页的题库列表
     */
    @GetMapping("/getLibrariesBySubject")
    public ApiResponse<PageInfo<QuestionLibrary>> getLibrariesBySubject(
            @RequestParam("subjectId") Long subjectId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<QuestionLibrary> pageInfo = libraryService.getLibraryListBySubject(subjectId, pageNum, pageSize);
        return ApiResponse.success(pageInfo);
    }

    /**
     * 删除题库
     * @param libraryId 题库id
     * @return 操作结果
     */
    @GetMapping("/deleteLibrary")
    public ApiResponse<Boolean> deleteLibrary(@RequestParam("libraryId") String libraryId) {
        boolean result = libraryService.deleteLibrary(libraryId);
        return ApiResponse.success(result);
    }


    /**
     * 添加题目到题库
     * @param libraryId 题库id
     * @param questionIds 题目id列表
     * @return 更新后的题库信息
     */
    @GetMapping("/addQuestion")
    public ApiResponse<QuestionLibrary> addQuestion(@RequestParam("libraryId") String libraryId,
                                                    @RequestParam("questionIds") List<Long> questionIds) {
        QuestionLibrary library = libraryService.addQuestionsToLibrary(libraryId, questionIds);
        return ApiResponse.success(library);
    }

    /**
     * 从题库中删除题目
     * @param libraryId 题库id
     * @param questionIds 题目id列表
     * @return 更新后的题库信息
     */
    @GetMapping("/deleteQuestion")
    public ApiResponse<QuestionLibrary> deleteQuestion(@RequestParam("libraryId") String libraryId,
                                                      @RequestParam("questionIds") List<Long> questionIds) {
        QuestionLibrary library = libraryService.deleteQuestionsFromLibrary(libraryId, questionIds);
        for (Long questionId : questionIds) {
            questionService.deleteQuestionById(questionId);
        }
        return ApiResponse.success(library);
    }

    /**
     * 获取题库中的题目
     * @param libraryId 题库id
     * @return 题目列表
     */
    @GetMapping("/getLibraryQuestions")
    public ApiResponse<List<QuestionDetail>> getLibraryQuestions(@RequestParam("libraryId") String libraryId) {
        List<QuestionDetail> questions = libraryService.getLibraryQuestions(libraryId);
        return ApiResponse.success(questions);
    }

    /**
     *  已实现
     * @param ids 根据id批量获取试题
     * @return 所有结果，前端分页
     */
    @GetMapping("/questions")
    public ApiResponse<List<QuestionDetail> > getQuestions(
            @RequestParam List<Long> ids) {
        return ApiResponse.success(questionService.getQuestionsByIds(ids));
    }

    /**
     * 获取题库信息
     * @param libraryId 题库id
     * @return 题库信息
     */
    @GetMapping("/getLibrary")
    public ApiResponse<QuestionLibrary> getLibrary(@RequestParam("libraryId") String libraryId) {
        QuestionLibrary library = libraryService.getLibraryById(libraryId);
        return ApiResponse.success(library);
    }

}